package com.sitech.ibnms.util;

import java.util.HashMap;
import java.util.Map;
import java.util.Queue;
import java.util.concurrent.LinkedBlockingQueue;

public class MsgQueueUtil {

	private static Map<String, Queue<String>> msgQueueMap = new HashMap<String, Queue<String>>();

	public static synchronized Queue<String> getMsgQueue(String guid) {
		Queue<String> msgQueue = msgQueueMap.get(guid);
		if (msgQueue == null) {
			msgQueue = new LinkedBlockingQueue<String>();
			msgQueueMap.put(guid, msgQueue);
		}
		return msgQueue;
	}
	
	public static String getMsg(String guid) {
		Queue<String> msgQueue = getMsgQueue(guid);

		String msg = null;
		do {
			msg = msgQueue.poll();
		} while (msg == null);

		if (msg.equals("THE END")) {
			msgQueue.clear();
			msgQueueMap.remove(guid);
		}
		return msg;
	}
}
